//
//  AddViewController.m
//  Japanese+
//
//  Created by Jame on 15/10/24.
//  Copyright © 2015年 SSIC. All rights reserved.
//http://120.27.163.106:8080/Japanese/dictionaries/queryDictionariesList?JB=2&PARENT_ID=d33a953a8da340639acc51c1123e7e28  上课方式

#import "AddViewController.h"

#import "AddImageViewController.h"
#import "AddVideoViewController.h"
#import "AddNameViewController.h"
#import "AddPriceViewController.h"
#import "AddSuitViewController.h"
#import "AddTimeViewController.h"
#import "AddTypeViewController.h"
#import "ZLPhoto.h"
#import "CaptureViewController.h"
#import "Common.h"
#import "SVProgressHUD.h"
#import <MediaPlayer/MediaPlayer.h>

#define KCachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]


@interface AddViewController () <UITextViewDelegate,UIAlertViewDelegate,ZLPhotoPickerViewControllerDelegate,UIActionSheetDelegate>

@end

@implementation AddViewController
{
    UIScrollView *_scrollView;
    UIView *_addVideoView;
    UIView *_addImageView;
    UIView *_nameView;
    UIView *_priceView;
    UIView *_suitView;
    UIView *_contentView;
//    UIView *_timeView;
    UIView *_typeView;
    
    UILabel *_addVideo;
    UILabel *_addImage;
    UILabel *_addName;
    UILabel *_addPrice;
    UILabel *_addSuit;
//    UILabel *_addTime;
    UILabel *_addType;
    UITextView *_contentText;
    CGRect rect;
    
    UIButton *VideoImageBtn;
    UIImageView *VideoImage;

    
    UIActionSheet *_photoSheet;
    UIActionSheet *_videoSheet;
    
    MemberModel *_memberModel;
    TeacherModel *_teacherModel;
    NSString *_lng;
    NSString *_lat;
    
    NSMutableArray *files;
    NSMutableArray *_imageArray;
    NSMutableArray *_videoArray;
    NSString *videoPath;
    NSData *videoData;
    
    NSString *videoPlayUrl;
    NSString *videoID;


    UIScrollView *_photoScroll;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"添加课程"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    
    [self creatScrollView];
    [self creatAddVideoView];
    [self creatAddImageView];
    [self creatNameView];
    [self creatPriceView];
    [self creatSuitView];
    [self creatContentView];
//    [self creatTimeView];
    [self creatTypeView];
    [self creatSubmitButton];
    videoID=@"";
}


#pragma mark - 视图即将加载
- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keybordFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [super viewWillAppear:YES];
    [self loadUserInfo];
}


- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
    _teacherModel = userInfo.teacherModel;
    _lat = userInfo.lat;
    _lng = userInfo.lng;
    
}


#pragma mark - 改变键盘位置
- (void)keybordFrameChange:(NSNotification *)noti
{
    NSValue *value = [noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    [value getValue:&rect];
    [UIView animateWithDuration:0.25 delay:0 options:7 animations:^{
        [_scrollView setContentOffset:CGPointMake(0, ECSCREEN_H - rect.origin.y) animated:YES];
    } completion:nil];
}

#pragma mark - 视图消失
- (void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [super viewDidDisappear:YES];
}


- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)creatScrollView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64 - 55)];
//    _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 579);
    _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 485);
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchScrollView)];
    [recognizer setNumberOfTapsRequired:1];
    [recognizer setNumberOfTouchesRequired:1];
    [_scrollView addGestureRecognizer:recognizer];
}


- (void)touchScrollView
{
    [_contentText resignFirstResponder];
}

- (void)creatAddVideoView
{
    _addVideoView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, ECSCREEN_W - 20, 88)];
    _addVideoView.clipsToBounds = YES;
    _addVideoView.layer.cornerRadius = 8;
    _addVideoView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_addVideoView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 65, 30)];
    label.text = @"添加视频";
    label.font = [UIFont systemFontOfSize:13.f];
    [_addVideoView addSubview:label];
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 44, 44)];
    image.image = ECIMAGENAME(@"reson_photo.png");
    [_addVideoView addSubview:image];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 40, 44, 44);
    button.tag = 111;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_addVideoView addSubview:button];
    
    
    VideoImage = [[UIImageView alloc] initWithFrame:CGRectMake(60, 40, 44, 44)];
    VideoImage.image = ECIMAGENAME(@"placeImage.png");
    [_addVideoView addSubview:VideoImage];
    VideoImage.hidden=YES;
    
    VideoImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    VideoImageBtn.frame = CGRectMake(60, 40, 44, 44);
    [VideoImageBtn addTarget:self action:@selector(VideoImageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_addVideoView addSubview:VideoImageBtn];
    VideoImageBtn.hidden=YES;
}


- (void)creatAddImageView
{
    _addImageView = [[UIView alloc] initWithFrame:CGRectMake(10, 98, ECSCREEN_W - 20, 88)];
    _addImageView.clipsToBounds = YES;
    _addImageView.layer.cornerRadius = 8;
    _addImageView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_addImageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 65, 30)];
    label.text = @"添加图片";
    label.font = [UIFont systemFontOfSize:13.f];
    [_addImageView addSubview:label];

    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 44, 44)];
    image.image = ECIMAGENAME(@"reson_photo.png");
    [_addImageView addSubview:image];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 40, 44, 44);
    button.tag = 112;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_addImageView addSubview:button];
    
    
    _photoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(64, 40, ECSCREEN_W - 20 - 44 - 30, 44)];
    _photoScroll.showsHorizontalScrollIndicator = NO;
    [_addImageView addSubview:_photoScroll];
}


- (void)creatNameView
{
    _nameView = [[UIView alloc] initWithFrame:CGRectMake(10, 191, ECSCREEN_W - 20, 44)];
    _nameView.clipsToBounds = YES;
    _nameView.layer.cornerRadius = 8;
    _nameView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_nameView];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, 65, 30)];
    label.text = @"课程名称";
    label.font = [UIFont systemFontOfSize:13.f];
    [_nameView addSubview:label];
    
    _addName = [[UILabel alloc] initWithFrame:CGRectMake(70, 7, ECSCREEN_W - 75 - 60, 30)];
    _addName.font = [UIFont systemFontOfSize:13.f];
    [_nameView addSubview:_addName];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 60, 7, 30, 30)];
    image.image = ECIMAGENAME(@"add_right.png");
    [_nameView addSubview:image];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, ECSCREEN_W-20, 44);
    button.tag = 113;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_nameView addSubview:button];
}


- (void)creatPriceView
{
    _priceView = [[UIView alloc] initWithFrame:CGRectMake(10, 240, ECSCREEN_W - 20, 44)];
    _priceView.clipsToBounds = YES;
    _priceView.layer.cornerRadius = 8;
    _priceView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_priceView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, 65, 30)];
    label.text = @"收费价格";
    label.font = [UIFont systemFontOfSize:13.f];
    [_priceView addSubview:label];
    
    
    _addPrice = [[UILabel alloc] initWithFrame:CGRectMake(70, 7, ECSCREEN_W - 75 - 60, 30)];
    _addPrice.font = [UIFont systemFontOfSize:13.f];
    [_priceView addSubview:_addPrice];
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 60, 7, 30, 30)];
    image.image = ECIMAGENAME(@"add_right.png");
    [_priceView addSubview:image];


    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, ECSCREEN_W-20, 44);
    button.tag = 114;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_priceView addSubview:button];

}


- (void)creatSuitView
{
    _suitView = [[UIView alloc] initWithFrame:CGRectMake(10, 289, ECSCREEN_W - 20, 44)];
    _suitView.clipsToBounds = YES;
    _suitView.layer.cornerRadius = 8;
    _suitView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_suitView];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, 65, 30)];
    label.text = @"适用人群";
    label.font = [UIFont systemFontOfSize:13.f];
    [_suitView addSubview:label];

    
    _addSuit = [[UILabel alloc] initWithFrame:CGRectMake(70, 7, ECSCREEN_W - 75 - 60, 30)];
    _addSuit.font = [UIFont systemFontOfSize:13.f];
    [_suitView addSubview:_addSuit];

    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 60, 7, 30, 30)];
    image.image = ECIMAGENAME(@"add_right.png");
    [_suitView addSubview:image];

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, ECSCREEN_W-20, 44);
    button.tag = 115;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_suitView addSubview:button];

}


- (void)creatContentView
{
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(10, 338, ECSCREEN_W - 20, 88)];
    _contentView.clipsToBounds = YES;
    _contentView.layer.cornerRadius = 8;
    _contentView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_contentView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, ECSCREEN_W/2, 30)];
    label.text = @"课程内容编辑";
    label.font = [UIFont systemFontOfSize:13.f];
    [_contentView addSubview:label];
    
    
    _contentText = [[UITextView alloc] initWithFrame:CGRectMake(15, 35, ECSCREEN_W - 50, 44)];
    _contentText.delegate = self;
    [_contentView addSubview:_contentText];
}


- (void)textViewDidBeginEditing:(UITextView *)textView
{
    UIButton *image = (UIButton *)[self.view viewWithTag:111];
    UIButton *video = (UIButton *)[self.view viewWithTag:112];
    UIButton *name = (UIButton *)[self.view viewWithTag:113];
    UIButton *price = (UIButton *)[self.view viewWithTag:114];
    UIButton *suit = (UIButton *)[self.view viewWithTag:115];
    UIButton *time = (UIButton *)[self.view viewWithTag:116];
    UIButton *type = (UIButton *)[self.view viewWithTag:117];
    UIButton *submit = (UIButton *)[self.view viewWithTag:118];
    
    _scrollView.scrollEnabled = NO;
    image.enabled = NO;
    video.enabled = NO;
    name.enabled = NO;
    price.enabled = NO;
    suit.enabled = NO;
    time.enabled = NO;
    type.enabled = NO;
    submit.enabled = NO;
}


- (void)textViewDidEndEditing:(UITextView *)textView
{
    UIButton *image = (UIButton *)[self.view viewWithTag:111];
    UIButton *video = (UIButton *)[self.view viewWithTag:112];
    UIButton *name = (UIButton *)[self.view viewWithTag:113];
    UIButton *price = (UIButton *)[self.view viewWithTag:114];
    UIButton *suit = (UIButton *)[self.view viewWithTag:115];
    UIButton *time = (UIButton *)[self.view viewWithTag:116];
    UIButton *type = (UIButton *)[self.view viewWithTag:117];
    UIButton *submit = (UIButton *)[self.view viewWithTag:118];

    _scrollView.scrollEnabled = YES;
    image.enabled = YES;
    video.enabled = YES;
    name.enabled = YES;
    price.enabled = YES;
    suit.enabled = YES;
    time.enabled = YES;
    type.enabled = YES;
    submit.enabled = YES;
}


//- (void)creatTimeView
//{
//    _timeView = [[UIView alloc] initWithFrame:CGRectMake(10, 431, ECSCREEN_W - 20, 44)];
//    _timeView.clipsToBounds = YES;
//    _timeView.layer.cornerRadius = 8;
//    _timeView.backgroundColor = ECCOLOR(255, 255, 255, 1);
//    [_scrollView addSubview:_timeView];
//    
//    
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, 65, 30)];
//    label.text = @"上课时间";
//    label.font = [UIFont systemFontOfSize:13.f];
//    [_timeView addSubview:label];
//    
//    
//    _addTime = [[UILabel alloc] initWithFrame:CGRectMake(70, 7, ECSCREEN_W - 75 - 60, 30)];
//    _addTime.font = [UIFont systemFontOfSize:13.f];
//    [_timeView addSubview:_addTime];
//
//    
//    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 60, 7, 30, 30)];
//    image.image = ECIMAGENAME(@"add_right.png");
//    [_timeView addSubview:image];
//
//    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(0, 0, ECSCREEN_W-20, 44);
//    button.tag = 116;
//    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//    [_timeView addSubview:button];
//
//}


- (void)creatTypeView
{
    _typeView = [[UIView alloc] initWithFrame:CGRectMake(10, 431, ECSCREEN_W - 20, 44)];
//    _typeView = [[UIView alloc] initWithFrame:CGRectMake(10, 480, ECSCREEN_W - 20, 44)];
    _typeView.clipsToBounds = YES;
    _typeView.layer.cornerRadius = 8;
    _typeView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_typeView];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, ECSCREEN_W/2, 30)];
    label.text = @"上课方式";
    label.font = [UIFont systemFontOfSize:13.f];
    [_typeView addSubview:label];
    
    
    _addType = [[UILabel alloc] initWithFrame:CGRectMake(70, 7, ECSCREEN_W - 75 - 60, 30)];
    _addType.font = [UIFont systemFontOfSize:13.f];
    [_typeView addSubview:_addType];

    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 60, 7, 30, 30)];
    image.image = ECIMAGENAME(@"add_right.png");
    [_typeView addSubview:image];

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, ECSCREEN_W-20, 44);
    button.tag = 117;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_typeView addSubview:button];

}


- (void)buttonClick:(UIButton *)button
{
//    AddImageViewController *ivc = [[AddImageViewController alloc] init];
//    AddVideoViewController *vvc = [[AddVideoViewController alloc] init];
    AddNameViewController *nvc = [[AddNameViewController alloc] init];
    AddPriceViewController *pvc = [[AddPriceViewController alloc] init];
    AddSuitViewController *svc = [[AddSuitViewController alloc] init];
    AddTimeViewController *timevc = [[AddTimeViewController alloc] init];
    AddTypeViewController *typevc = [[AddTypeViewController alloc] init];
    
    switch (button.tag) {
        case 111:
        {
            NSLog(@"video");
//            [self.navigationController pushViewController:vvc animated:YES];
            _videoSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"录像机",nil];
            [_videoSheet showInView:self.view];
        }
            break;
        case 112:
        {
            NSLog(@"image");
//            [self.navigationController pushViewController:ivc animated:YES];
            _photoSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"我的相册", @"照相机",nil];
            [_photoSheet showInView:self.view];
        }
            break;
        case 113:
        {
            
            NSLog(@"name");
            [nvc setAddName:^(NSString *name) {
                _addName.text = name;
            }];
            [self.navigationController pushViewController:nvc animated:YES];
        }
            break;
        case 114:
        {
            NSLog(@"price");
            [pvc setAddPrice:^(NSString *price) {
                _addPrice.text = price;
            }];
            [self.navigationController pushViewController:pvc animated:YES];
        }
            break;
        case 115:
        {
            NSLog(@"suit");
            [svc setAddSuit:^(NSString *suit) {
                _addSuit.text = suit;
            }];
            [self.navigationController pushViewController:svc animated:YES];
        }
            break;
        case 116:
        {
//            NSLog(@"time");
//            [timevc setAddTime:^(NSString *time) {
//                _addTime.text = time;
//            }];
//            [self.navigationController pushViewController:timevc animated:YES];
        }
            break;
        case 117:
        {
            NSLog(@"type");
            [typevc setAddType:^(NSString *type) {
                _addType.text = type;
            }];
            [self.navigationController pushViewController:typevc animated:YES];
        }
            break;

            
        default:
            break;
    }
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet == _photoSheet) {
        switch (buttonIndex) {
            case 0:
            {
                ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
                // 默认显示相册里面的内容SavePhotos
                pickerVc.maxCount = 6;
                pickerVc.status = PickerViewShowStatusCameraRoll;
                pickerVc.delegate = self;
                [pickerVc showPickerVc:self];
            }
                break;
            case 1:
            {
                ZLCameraViewController *cameraVc = [[ZLCameraViewController alloc] init];
                cameraVc.maxCount = 6;
                cameraVc.callback = ^(id object){
                    
                    [SVProgressHUD showWithStatus:@"图片上传中..."];
                    
                    if (_imageArray) {
                        [_imageArray removeAllObjects];
                    }else{
                        _imageArray = [[NSMutableArray alloc] init];
                    }
                    
                    [self refreshScrollViewWithArray:(NSArray *)object WithCamara:YES];
                    [self uploadPhotoWithArray:(NSArray *)object WithCamara:YES];
                };
                [cameraVc showPickerVc:self];
            }
                break;

                
            default:
                break;
        }
    }else{
        switch (buttonIndex) {
            case 0:
            {
//                ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
//                // 默认显示相册里面的内容SavePhotos
//                // 最多能选9张图片
//                pickerVc.maxCount = 1;
//                pickerVc.status = PickerViewShowStatusVideo;
//                pickerVc.delegate = self;
//                [pickerVc showPickerVc:self];
                NSLog(@"打开录像机！");
                UINavigationController *navCon = [[UINavigationController alloc] init];
                navCon.navigationBarHidden = YES;
                
                CaptureViewController *captureViewCon = [[CaptureViewController alloc] init];
                [navCon pushViewController:captureViewCon animated:NO];
                [captureViewCon setVideoDelegate:(id<VideoBackDeleate>)self];
                [self presentViewController:navCon animated:YES completion:nil];
            }
                break;
//            case 1:
//            {
//                NSLog(@"打开录像机！");
//                UINavigationController *navCon = [[UINavigationController alloc] init];
//                navCon.navigationBarHidden = YES;
//                
//                CaptureViewController *captureViewCon = [[CaptureViewController alloc] init];
//                [navCon pushViewController:captureViewCon animated:NO];
//                [captureViewCon setVideoDelegate:(id<VideoBackDeleate>)self];
//                [self presentViewController:navCon animated:YES completion:nil];
//            }
                
            default:
                break;
        }
    }
}


- (void)pickerViewControllerDoneAsstes:(NSArray *)assets{
    [SVProgressHUD showWithStatus:@"图片上传中..."];
    NSLog(@"%@",assets);
    
    if (_imageArray) {
        [_imageArray removeAllObjects];
    }else{
        _imageArray = [[NSMutableArray alloc] init];
    }
    
    [self refreshScrollViewWithArray:assets WithCamara:NO];
    [self uploadPhotoWithArray:assets WithCamara:NO];

}



- (void)uploadPhotoWithArray:(NSArray *)array WithCamara:(BOOL)isCamara
{
    if (isCamara) {
        for (ZLCamera *camera in array) {
            
            if (files) {
                [files removeAllObjects];
            }else{
                files = [[NSMutableArray alloc] init];
            }

            NSData *data = UIImageJPEGRepresentation(camera.photoImage, 0.75);
            NSString *strRandomName = [NSString stringWithFormat:@"iOS_uploadImage_%d.jpg",arc4random_uniform(10000)];
            [files addObject:@{
                               @"name": strRandomName,
                               @"file": data,
                               @"key": @"image",
                               @"type": @"multipart/form-data"
                               }
             ];
            
            [[HttpManager defaultManager] uploadToUrl:RequestURL(POSTPIC) params:nil files:files complete:^(BOOL successed, NSDictionary *result) {
                if (successed) {
                    if ([result[@"errCode"] integerValue] == 0) {
                        [_imageArray addObject:result[@"pictures_ID"]];
                        if (_imageArray.count==array.count) {
                            NSLog(@"******%@********",_imageArray);
                            [SVProgressHUD dismissWithSuccess:@"上传成功！"];
                        }
                    }
                }
            }];

        }
        
        
    }else{
        for (ZLPhotoAssets *asset in array) {
            
            if (files) {
                [files removeAllObjects];
            }else{
                files = [[NSMutableArray alloc] init];
            }
            
            NSData *data = UIImageJPEGRepresentation(asset.originImage, 0.75);
            NSString *strRandomName = [NSString stringWithFormat:@"iOS_uploadImage_%d.jpg",arc4random_uniform(10000)];
            [files addObject:@{
                               @"name": strRandomName,
                               @"file": data,
                               @"key": @"image",
                               @"type": @"multipart/form-data"
                               }
             ];
            
            [[HttpManager defaultManager] uploadToUrl:RequestURL(POSTPIC) params:nil files:files complete:^(BOOL successed, NSDictionary *result) {
                if (successed) {
                    if ([result[@"errCode"] integerValue] == 0) {
                        [_imageArray addObject:result[@"pictures_ID"]];
                        if (_imageArray.count==array.count) {
                            NSLog(@"******%@********",_imageArray);
                            [SVProgressHUD dismissWithSuccess:@"上传成功！"];
                        }
                    }
                }
            }];
        }
    }
    
}



- (void)refreshScrollViewWithArray:(NSArray *)array WithCamara:(BOOL)isCamara
{
    NSArray *subViews = [_photoScroll subviews];
    
    if([subViews count] != 0) {
        [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    if (isCamara) {
        int i = 0;
        for (ZLCamera *camera in array) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(54 * i, 0, 44, 44)];
            imageView.image = camera.photoImage;
            [_photoScroll addSubview:imageView];
            i++;
            _photoScroll.contentSize = CGSizeMake(55 * i, 44);
        }
    }else{
        int i = 0;
        for (ZLPhotoAssets *asset in array) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(54 * i, 0, 44, 44)];
            imageView.image = asset.originImage;
            [_photoScroll addSubview:imageView];
            i++;
            _photoScroll.contentSize = CGSizeMake(55 * i, 44);
        }
    }
}


-(void) videoBackDelegateWithUrl:(NSURL *)videoUrl videoImageArr:(NSArray*)ImageArr withImageFrmae:(CGRect)frame{
//- (void)videoBackDelegateWithUrl:(NSURL *)videoUrl{

    NSLog(@"videourl=====%@",videoUrl);
    NSLog(@"ImageArr=====%@",ImageArr);
    NSLog(@"frame=====%@",frame);
    
    videoPath = [videoUrl absoluteString];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    path = [path stringByAppendingPathComponent:VIDEO_FOLDER];
    NSString *fileName = [[path stringByAppendingPathComponent:@"videoMerge"] stringByAppendingString:@".mp4"];
    
    
    //    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"final_video" ofType:@"mp4"];
    
    videoData = [NSData dataWithContentsOfFile:fileName];
    NSString *strRandomName = [NSString stringWithFormat:@"iOS_uploadVideo_%d.mp4",arc4random_uniform(10000)];
    NSMutableArray *file = [[NSMutableArray alloc] init];
    [file addObject: @{
                       @"name": strRandomName,
                       @"file": videoData,
                       @"key": @"image",
//                       @"type": @"video/mpeg4"
                       @"type": @"multipart/form-data"
                       }];
    [[HttpManager defaultManager] uploadToUrl:RequestURL(POSTPIC) params:nil files:file complete:^(BOOL successed, NSDictionary *result) {
        NSLog(@"%@",result);
        if (successed) {
            if ([result[@"errCode"] integerValue] == 0) {
//                [_imageArray addObject:result[@"pictures_ID"]];
                NSLog(@"%@",result);
                [SVProgressHUD dismissWithSuccess:@"上传成功！"];
                videoPlayUrl=[result objectForKey:@"path"];
                videoID=[result objectForKey:@"pictures_ID"];
                if (ImageArr.count) {
                    UIImage *imge=(UIImage*)[ImageArr objectAtIndex:0];
                    [VideoImage setImage:imge];
                    VideoImage.hidden=NO;
                }
                if ( [[NSFileManager defaultManager] fileExistsAtPath:fileName]) {
                    NSFileManager *fileManager = [NSFileManager defaultManager];
                    [fileManager removeItemAtPath:fileName error:nil];
                }
                VideoImageBtn.hidden=NO;
            }
        }else{
            [SVProgressHUD dismissWithSuccess:@"上传失败！"];
        }
    }];
}
-(void)VideoImageBtnClick{
    if ([videoPath length]) {
        MPMoviePlayerViewController *playerViewController =[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:videoPlayUrl]];
        [self presentMoviePlayerViewControllerAnimated:playerViewController];
    }
}



- (void)creatSubmitButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = 118;
    button.frame = CGRectMake(10, ECSCREEN_H - 50, ECSCREEN_W - 20, 40);
    button.clipsToBounds = YES;
    button.layer.cornerRadius = 5;
    button.backgroundColor = NAVC_COLOR;
    [button setTitle:@"下一步" forState:UIControlStateNormal];
    [button setTitleColor:ECCOLOR(255, 255, 255, 1) forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [button addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}


- (void)submitClick:(UIButton *)button
{
    if (_addName.text.length == 0) {
        SHOW_ALERT(@"提示", @"请输入课程名称", @"好的", nil, nil);
    }else if (_addPrice.text.length == 0){
        SHOW_ALERT(@"提示", @"请输入课程价格", @"好的", nil, nil);
    }else if (_addSuit.text.length == 0){
        SHOW_ALERT(@"提示", @"请输入适用人群", @"好的", nil, nil);
    }else if (_contentText.text.length == 0){
        SHOW_ALERT(@"提示", @"请输入课程内容", @"好的", nil, nil);
    }else if (_addType.text.length == 0){
        SHOW_ALERT(@"提示", @"请输入上课方式", @"好的", nil, nil);
    }else{
//        NSString *time = [_addTime.text stringByReplacingOccurrencesOfString:@" " withString:@"T"];
//        time = [NSString stringWithFormat:@"%@:00",time];
//        NSLog(@"%@",time);
        
        NSString *imgId;
        if (_imageArray.count > 0) {
            if (_imageArray.count == 1) {
                imgId = _imageArray[0];
            }else{
                imgId = [_imageArray componentsJoinedByString:@","];
            }
        }else{
            imgId = @"";
        }
        
//        if (_videoArray.count > 0) {
//            videoID = _videoArray[0];
//        }else{
//            videoId = @"";
//        }
        
        NSDictionary *data = @{
                               @"promulgator_Id":_memberModel.member_Id,
                               @"t_member_Id":_memberModel.member_Id,
                               @"promulgator_mark":@"1",
                               @"course_name":_addName.text,
                               @"price":_addPrice.text,
                               @"suits_the_crowd":_addSuit.text,
                               @"course_content":_contentText.text,
//                               @"class_time":@"",
                               @"have_class_way":_addType.text,
                               @"lng":_lng,
                               @"lat":_lat,
                               @"status":@"1",
                               @"course_ImgIds":imgId,
                               @"course_video_Ids":videoID,
                               };
        
        AddTimeViewController *vc = [[AddTimeViewController alloc] init];
        vc.data = data;
        [self.navigationController pushViewController:vc animated:YES];
//        NSDictionary *param = @{
//                                @"data":data,
//                                };
//        
//        [[HttpManager defaultManager] postRequestToUrl:RequestURL(UPDATECOURSE) params:param complete:^(BOOL successed, NSDictionary *result) {
//            if (successed) {
//                if ([result[@"errCode"] integerValue] == 200) {
//                    SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, self);
//                }else{
//                    SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, nil);
//                }
//            }
//        }];
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
